var user = angular.module("userapp", ['ngRoute']);

user.config(function($routeProvider) {
	$routeProvider.when('/home', {
		templateUrl: base_url + "/index.php" + "/user/home",
		controller: "homeCtrl"
	})
	.when('/profile', {
		templateUrl: base_url + "index.php" + "/user/profile",
		controller: "profileCtrl"
	})
	.when('/editProfile',{
		templateUrl: base_url + "index.php" + "/user/editProfile",
		controller: "editCtrl"
	})
	.when('/login',{
		templateUrl: base_url + "index.php" + "/user/login",
		controller: "loginCtrl"
	})
	.when('/logout',{
		templateUrl: base_url + "index.php" + "/user/logout",
		controller: "loginCtrl"
	})
	.when('/register', {
		templateUrl: base_url + "index.php" + "/user/register",
		controller: "registerCtrl"
	})
	.otherwise({ redirectTo: '/login' });
});

user.constant('BASE_URL', base_url);

user.run(function($rootScope, $location, loginService){
	var routespermission=['/home'];  //route that require login
	$rootScope.$on('$routeChangeStart', function(event){

		// if( routespermission.indexOf($location.path()) !=-1)
		// {
		// 	var connected=loginService.islogged();
		// 	connected.then(function(msg){
		// 		if(!msg.data) 
		// 			//$location.path('/home');
		// 			//window.location.href ="http://localhost/thesis1";
		// 			window.location.href = base_url;

		// 			$rootScope.islogged = true;
		// 	});
		// }

		loginService.islogged().then(function(promise) {
			if(promise.data == "") {
				// $rootScope.islogged = false;
				$rootScope.$broadcast('islogged', false);
				$location.path('/login');
			}
			else {
				$rootScope.$broadcast('islogged', true);
			}
		});
	});
});

function GlobalController($scope, $http, $location, $rootScope, loginService) {
	$scope.$on('islogged', function(event, data) {
		$scope.islogged = data;
	})
	$scope.redirect = function(view) {
		$location.path('/' + view);
	}
}
